-
You're welcome, Dakaa.
To set a variable to zero, it's = 0.
'0' is the char of 0, and has the value of ascii 48, iirc.
The computer world is your world - do it the way you want to do it. I'd suggest fgets(with stdin as the filename), because it lets you limit the amount of char's going into your buffer.
But you could also get 1 digit as a char, at a time, and use a for loop to control it.
C has a function to check if a char is a number or not. isdigit(), and you'll need ctype.h as an include file.
-
*sigh*, been doing this all day, error number 2 is killing me.
Code:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
int main()
{
char cISBN[10];
int i,iLen,iNNF,iISBN[9],iTotal;
for(i=0;i<10;i++)
{
cISBN[i]='\0';
}
printf("Enter ISBN to calculate Check Digit ");
gets(cISBN);
iLen=strlen(cISBN);
if(iLen!=9)
{
printf("ISBN length error ");
}
else
{
iNNF=0;
for(i=0;i<iLen&&iNNF==0;i++)
{
if((cISBN[i]<0)||(cISBN[i]>9))
{
printf("ISBN numerical error ");
iNNF=1;
}
else
{
iISBN[i]=cISBN[i]-48;
iTotal+=iISBN[i]*(i+1);
}
}
}
printf("%d",(iTotal%11));
getch();
return 0;
}
/*something is wrong in BOLD, the output is either "ISBN numerical error 10" or "ISBN length error 10" and I don't know what the hell the 10 is...*/
iNNF is Non-Numerical Flag.
-
Code:
char cISBN[9];
for(i=0;i<10;i++)
{
cISBN[i]='\0';
}
If you are going to access elements 0..9, then you need to have at least 10 elements in the array - you only have 9.
Further, an old ISBN number (ISBN-10) is 10 digits long. If you want to have a C-style STRING containing a 10 digit "number", then you need an array of 11 chars, since you need 10 characters of the ISBN _and_ the zero to indicate the end of the string.
Also, do not use gets() to read the string in - if someone enters a new-steyle 13-digit ISBN, your program is likely to crash (or at least do strange things). Use fgets() or some other function that has can stop inputting when there is no space to store the string.
--
Mats
-
cISBN[10], 9 characters of ISBN and 1 empty space, thanks for pointing that out. I'm trying to do it the noob way my teacher taught me, but the output is weird and we are doing the old style ISBN, 9 Digits + 1 Check Digit.
-
-